/**
 * Created by Administrator on 2017/4/16.
 */
!(function($){
    var detail = {
        userId: $.fn.cookie('userId'),
        isLogin: $.fn.cookie('isLogin'),
        init:function(){
            this.getParams();
            this.getDetailInfo();
            this.getMessages();
            this.liveMessage();
            this.addFavorite();
            this.saveUrl();
            this.isHasPhonenum();
        },
        saveUrl:function(){
            $.fn.cookie('curUrl',window.location.href);
        },
        isFav:function(){
            var isFavorite = $('#isFavorite').val();
            if(isFavorite){
                if(isFavorite ==1){
                    $('.icon.icon-fav').addClass('icon-fav-over').removeClass('icon-fav');
                }else{
                    $('.icon.icon-fav').addClass('icon-fav').removeClass('icon-fav-over');
                }
            }
        },
        getParams:function(){
           var idv =  GetQueryString('id');
           $('#id').val(idv);
        },
        isHasPhonenum:function(){
          $('#contact_phone').on('tap',function(){
              if(!$('#contact_phone').attr('href')){
                  $.alert('此商家未留电话！请留言')
              }
          })
        },
        getDetailInfo:function(){
            var self = this;
            var id =  parseInt($('#id').val());
            var userId = self.userId;
            self.getUserStatus();
            $.ajax({
                url:API_URL+GET_SINGLE_PRO_URL,
                type:'get',
                dataType:'jsonp',
                data:{id:id,userId:userId},
                success:function(data){
                    if(data.ok==true){
                        if(data.result.user.phoneNum){
                            $('#contact_phone').attr('href','tel:'+data.result.user.phoneNum);
                        }
                        else{
                            $('#contact_phone').addClass('disabled');

                        }
                        var tpl = template('detail_tpl',data);
                        $('.product_detail').empty().html(tpl);
                        new Swiper('#slider .swiper-container', {
                            pagination: '#slider .swiper-pagination',
                            paginationClickable :true
                        });
                        self.isFav();
                    }else{
                        $.toptips(data.msg,'info');
                    }
                },
                error:function(data){
                    $.toptips(data.msg);
                }
            })
        },
        getMessages:function(){
            var id = parseInt($('#id').val());
            $.ajax({
                url:API_URL+GET_COMMENTS_URL,
                type:'get',
                dataType:'jsonp',
                data:{productId:id,page:1,pageSize:10},
                success:function(data){
                    if(data.ok==true){
                        var tpl = template('comments_tpl',data);
                        $('.messages').empty().html(tpl);
                    }else{
                        $.toptips(data.msg,'info');
                    }
                },
                error:function(data){
                    $.toptips(data.msg,'info');
                }
            })
        },
        addFavorite:function(){
            var self = this;
            var i = 1;
            $('#addfavorite').on('click',function(){
                var isFav =  $('#isFavorite').val();
                var isLogin = self.isLogin;
                if(isLogin){
                    i++;
                    var id =  parseInt($('#id').val());
                    var that = $(this);
                    if(isFav==0){
                        $.ajax({
                            url:API_URL+ADD_FAVORITE_URL,
                            type:'post',
                            dataType:"jsonp",
                            data:{userId:self.userId,id:id},
                            success:function(data){
                                if(data.ok==true){
                                    if(data.result==true){
                                        $.toast("收藏成功");
                                        that.find('.icon').removeClass('icon-fav').addClass('icon-fav-over');
                                        $('#isFavorite').val(1);
                                    }else{
                                        $.toptips(data.msg,'info');
                                    }
                                }
                            },
                            error:function(data){
                                $.toptips(data.msg);
                            }
                        })
                    }else{
                        $.ajax({
                            url:API_URL+DEL_FAVORITE_URL,
                            type:'post',
                            dataType:'jsonp',
                            data:{userId:self.userId,id:id},
                            success:function(data){
                                if(data.ok==true){
                                    if(data.result==true){
                                        $.toast("取消收藏");
                                        that.find('.icon').removeClass('icon-fav-over').addClass('icon-fav');
                                        $('#isFavorite').val(0);
                                    }else{
                                        $.toptips(data.msg,'info');
                                    }
                                }
                            },
                            error:function(data){
                                $.toptips(data.msg,'info');
                            }
                        })
                    }
                }else{
                    window.location.href = successUrl;
                }
            })

        },
        getUserStatus:function(){
            var self = this;
            $.ajax({
                url:API_URL+GET_USER_INFO,
                type:'post',
                dataType:'jsonp',
                data:{id:self.userId},
                success:function(data){
                    if(data.ok==true){
                        var datas = data.result;
                        if(datas.status==-1){
                            $.modal({
                                title: "您的账号已被停用！",
                                buttons: [
                                    { text: "知道了", onClick: function(){
                                        window.location.href='index.html';
                                    } },
                                    { text: "申诉", className: "default",onClick:function(){
                                        window.location.href='appeals.html';
                                    }}
                                ]
                            });
                        }
                        if(datas.status==0){
                            $.modal({
                                title: "您的账号已被冻结！！",
                                buttons: [
                                    { text: "知道了", onClick: function(){
                                        window.location.href='index.html';
                                    } },
                                    { text: "申诉", className: "default",onClick:function(){
                                        window.location.href='appeals.html';
                                    }}
                                ]
                            });
                        }
                    }
                }
            })
        },
        liveMessage:function(){
            var self = this;
            $(document).on("click", "#livemessage", function() {
                if(self.isLogin){
                    $.modal({
                        title: "留言",
                        text: '<div class="live_message weui_cell_bd weui_cell_primary">'+
                        '<textarea id="msg" class="weui_textarea" placeholder="请输入留言" rows="3"></textarea>'+
                        '</div>',
                        buttons: [
                            { text: "取消", onClick: function(){

                            } },
                            { text: "确认", className: "default",onClick:function(){
                                var msgs = $('#msg').val();
                                if(msgs!=''){
                                    $.ajax({
                                        url:API_URL+TO_COMMENTS_URL,
                                        type:'post',
                                        dataType:'jsonp',
                                        data:{
                                            productId:$('#id').val(),
                                            message:msgs,
                                            userId:self.userId
                                        },
                                        success:function(data){
                                            if(data.ok==true){
                                                $.toast("留言成功！");
                                                self.getMessages();
                                            }else{
                                                $.toptips("留言不成功！",'info');
                                                return;
                                            }
                                        },
                                        error:function(data){
                                            $.toptips(data.msg);
                                        }
                                    })
                                }else{
                                    $.toptips("请输入评论内容！")
                                }
                            }}
                        ]
                    });
                }else{
                    window.location.href = successUrl;
                }
            });

        }


    };
    $(function(){
        detail.init();
    })
})(Zepto);